package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.approve.api.IApl_arrive_detailFeign;
import com.power.sifa.approve.model.Apl_arrive_detail;
import com.power.sifa.approve.model.pageInfo.Apl_arrive_detailPageInfo;
import com.power.sifa.approve.model.pojo.vo.ReviewDetailVO;
import com.power.sifa.approve.service.Apl_arrive_detailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   送达记录详情控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_arrive_detailController",tags={"送达记录详情"})
public class Apl_arrive_detailController implements IApl_arrive_detailFeign {

    @Resource
    private Apl_arrive_detailService apl_arrive_detailService;

    
    @Override
    @ApiOperation("查询送达记录详情分页列表")
    @SaCheckPermission("apl_arrive_detail_list")
    public ResponseDTO list(Apl_arrive_detailPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_arrive_detail> page = this.apl_arrive_detailService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_arrive_detail apl_arrive_detail : page) {
                apl_arrive_detail.geneText();
                list.add(apl_arrive_detail);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看送达记录详情")
    @SaCheckPermission("apl_arrive_detail_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_arrive_detail apl_arrive_detail  = apl_arrive_detailService.getById(id);
        if (apl_arrive_detail != null) {
            apl_arrive_detail.geneText();
        }
        return ResponseDTO.succData(apl_arrive_detail);
    }

    @Override
    @ApiOperation("删除送达记录详情")
    @SaCheckPermission("apl_arrive_detail_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_arrive_detailService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改送达记录详情")
    @SaCheckPermission("apl_arrive_detail_save")
    public ResponseDTO save(Apl_arrive_detail apl_arrive_detail) {
        Assert.notNull(apl_arrive_detail, "apl_arrive_detail不能为空");
        apl_arrive_detailService.saveOrUpdate(apl_arrive_detail);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除送达记录详情")
    @SaCheckPermission("apl_arrive_detail_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_arrive_detailService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_arrive_detail_list")
    public void output(HttpServletResponse response, Apl_arrive_detailPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "送达记录详情";
        String[] heads = {"序号","applyid","创建时间","告警时间","完成时间","通知人id","通知人名称","是否自定义上传文件","送达方式","送达状态","送达时间","操作人员id","操作人员名称","邮寄单号"};
        String[] fields = {"id","applyid","createtime","warntime","finishtime","noticeperid","noticeper","customer","sendway","sendstatus","sendtime","operateid","opereatename","postcode"};
        Page<Apl_arrive_detail> page = this.apl_arrive_detailService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "送达记录详情";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","applyid","创建时间","告警时间","完成时间","通知人id","通知人名称","是否自定义上传文件","送达方式","送达状态","送达时间","操作人员id","操作人员名称","邮寄单号"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","applyid","createtime","warntime","finishtime","noticeperid","noticeper","customer","sendway","sendstatus","sendtime","operateid","opereatename","postcode"};
        List<Apl_arrive_detail> list = (List<Apl_arrive_detail>)ExcelUtil.Import(fields, file, Apl_arrive_detail.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_arrive_detailService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO saveTemp(List<Apl_arrive_detail> details) {
        try {
            this.apl_arrive_detailService.saveTemp(details);
            return ResponseDTO.succ();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.wrap(500, "送达记录暂存失败");
        }
    }

    @Override
    public ResponseDTO saveSend(List<Apl_arrive_detail> details, String sendUserId, String sendUserName) {
       try {
           this.apl_arrive_detailService.saveSend(details,sendUserId, sendUserName);
           return ResponseDTO.succ();
       } catch (Exception e) {
           e.printStackTrace();
           return ResponseDTO.wrap(500, "送达记录发送失败");
       }
    }

    @Override
    public ResponseDTO saveNewDetail(Apl_arrive_detail detail) {
        try {
            this.apl_arrive_detailService.saveNewDetail(detail);
            return ResponseDTO.succ();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.wrap(500, "送达记录新增失败");
        }
    }

    @Override
    public ResponseDTO getReviewDetailByApplyid(String applyid) {
        ReviewDetailVO result = this.apl_arrive_detailService.getReviewDetailByApplyid(applyid);
        return ResponseDTO.succData(result);
    }

    /**
     * 获取对应的文件树类型
     * @param applyid applyid
     * @return
     */
    @Override
    public ResponseDTO getDocumentTree(String applyid) {
        Assert.notNull(applyid, "applyid不能为空");
        return ResponseDTO.succData(this.apl_arrive_detailService.getDocumentTree(applyid));
    }

    @Override
    public ResponseDTO getByUserid(String userid) {
        try {
            return ResponseDTO.succData(this.apl_arrive_detailService.getByUserid(userid));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据userid查询待处理的通知失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "根据userid查询待处理的通知失败");
        }
    }

    @Override
    public ResponseDTO getByApplyid(String applyid) {
        return ResponseDTO.succData(this.apl_arrive_detailService.getByApplyid(applyid));
    }

    @Override
    public ResponseDTO deleteDetailById(String id) {
        try {
            this.apl_arrive_detailService.deleteDetailById(id);
            return ResponseDTO.succ();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除送达记录信息失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "删除送达记录信息失败");
        }
    }


}
